<?php
namespace app\index\controller\Project;
use app\lib\WordExport;
class ProjectWeekly extends base_project
{
    public $tableName = "ProjectWeekly";
    public function index()
    {
        $where["project_id"]=$this->base_project_id;
        if($this->allow_project_ids!=999){
            $where["uid"]=$this->member_id;
        }
        $this->page( $where, "", "", "id desc", "project");
        $data["week_day"]=date('W',time());
        $this->assign($data);
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {

        $model = model('ProjectWeekly');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            if (empty($post["project_id"])) {
                $this->error("所属项目不能为空");
            }
            $data["project_id"] = $post["project_id"];
          
            $month= date("Y");
            $data["weekly_info"] = $post["weekly_info"];
            $data["outsourcing"] = $post["outsourcing"];
            $data["jidong"] = $post["jidong"];
            $data["month"] =  $month;
            $ProjectWeeklyModel = model("ProjectWeekly");
            $count=   $ProjectWeeklyModel->where(["uid"=>$this->member_id,"project_id"=>$post["project_id"],"weeks"=> $post["weeks"],"month"=>$month])->count();
           if($count>0){
             $this->error("操作失败,项目当月周报已存在" );
           }
            // $data["duration"] = $post["duration"];
            //项目阶段
            //$data["project_stage"] = $post["project_stage"];
            $data["finance"] = $post["finance"];
            $data["project_progress"] = $post["project_progress"];
            $data["week_work"] = $post["week_work"];
            $data["quality_control"] = $post["quality_control"];
            $data["customer"] = $post["customer"];
            $data["product"] = $post["product"];
            $data["supplier"] = $post["supplier"];
            $data["resource"] = $post["resource"];
            $data["weeks"] = $post["weeks"];
            $data["uid"] = $this->member_id;
           // print_r($post["product_info"]);
        
            $result = $ProjectWeeklyModel->validate(true)->save($data);
            if (false === $result) {
                // 验证失败 输出错误信息
                $this->error("操作失败," . $ProjectWeeklyModel->getError());
            }
           $week_id= $ProjectWeeklyModel->id;
   
        if(!empty($post["month_content"])){
           $monthData["project_id"] =$this->base_project_id;
           $monthData["uid"] =    $this->member_id;
           $monthData["weekday"] =   $post["weeks"];
           $monthData["content"] = $post["month_content"];
           $monthData["week_id"] =$week_id;            
            model("Monthly")->save($monthData);
            
        }
            $product_info=$post["product_info"];
            $p_info_data=[];
            for($i=0;$i<count($product_info["title"]);$i++){
                $tmp_data["project_id"]=$post["project_id"];
                $tmp_data["week_id"]=$week_id;
                $tmp_data["title"]=$product_info["title"][$i];
                $tmp_data["provider"]=$product_info["provider"][$i];
                $tmp_data["price"]=floatval($product_info["price"][$i]);
                $tmp_data["purch_price"]=floatval($product_info["purch_price"][$i]);
                $tmp_data["business"]=$product_info["business"][$i];
                $tmp_data["description"]=$product_info["description"][$i];
                $tmp_data["resolve"]=$product_info["resolve"][$i];
                $tmp_data["uid"]=$this->member_id;
                $p_info_data[]= $tmp_data;                
         
            model("WeekProduct")-> saveAll($p_info_data);
            $this->success("操作成功", url("/Index/project/ProjectWeekly/index"));
        }
    }

        $project_id = $this->base_project_id;
        $project = [];
        $team_name = "";
        $pm_info = [];

        if (empty($project_id)) {
            $project = model("Project")->field("id,title,project_no")->select()->toArray();
        } else {
            $project = model("Project")->where(["id" => $project_id])->field("id,title,project_no,pm_uid,day")->find();
            $pm_info = model("Admin")->where(["id" => $project["pm_uid"]])->field('id,username,nickname')->find();
            $teams = model("Team")->where(["project_id" => $project_id])->with("user")->select()->toArray();
            if (!empty($teams)) {
                foreach ($teams as $tt) {
                    $team_name = $team_name . "," . $tt["user"]["nickname"];
                }
                $team_name = ltrim($team_name, ",");
                // $team_name=implode (",",array_column( $teams,"user"));
            }
        }
        $week_info=$model->where(["project_id"=>$project_id,"uid"=>$this->member_id])->order("id desc")->find();
        $week_product=[];
        if(!empty($week_info)){
            $week_product=     model("WeekProduct")->where(["project_id"=>$project_id,"uid"=>$this->member_id,"week_id"=>$week_info["id"]])->select();
        }
        $now_month=  date('m',time());
        $year=date('Y',time());
        $now_time=date("Y-m-d");
        $start_time=$now_month."-25";
        $end_time=($now_month+1)."-05";
        $show_month=false;
        if($now_time>=$year."-".$start_time&&$now_time<=$year."-".$end_time){
            $show_month=true;
        }
        $data["week_day"]=date('W',time());
        $data["week_info"] = $week_info;
        $data["show_month"] = $show_month;
        $data["week_product"] = $week_product;
        $data["project"] = $project;
        $data["pm_info"] = $pm_info;
        $data["team_name"] = $team_name;

        $data["project_id"] = $project_id;
        $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $ProjectWeeklyModel = model('ProjectWeekly');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["id"] = $post["id"];
            $data["project_id"] = $post["project_id"];
            $data["uid"] = $post["uid"];
            $data["content"] = $post["content"];
            $data["address"] = $post["address"];
            $data["night_shift"] = $post["night_shift"];
            $data["create_time"] = $post["create_time"];
            $data["update_time"] = $post["update_time"];
            $data["delete_time"] = $post["delete_time"];

            $map["id"] = $post["id"];
            $result = $this->operateData($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("Index/Menu/index"));
        }

        $id = input("id");

        $data["info"] = $ProjectWeeklyModel->get($id);

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        $project_id = $this->base_project_id;
        if (empty($id)) {
            $this->error("删除失败");
        }
        $result = $this->delBase(["id" => intval($id),"uid"=>$this->member_id]);
        if ($result) {
            model("WeekProduct")->where(["project_id"=>$project_id,"uid"=>$this->member_id,"week_id"=> $id ])->delete();
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

    /**
     * 项目经理导出周报
     */
    public  function export(){
       $word= new WordExport();
       $projectWeek= model("ProjectWeekly");
       $product= model("WeekProduct");
       $project= model("Project");
       $member= model("Admin");
       $week_id=input("week_id");
       $project_id = $this->base_project_id;
       $request =request();
       $domain= $request->domain();
       $url= $domain."/index/common/week_info?projetc_id=".$project_id."&week_id=".$week_id."&member_id=".$this->member_id;
       $info=file_get_contents($url);
       $str="";
       preg_match("/<body[^>]*?>(.*\s*?)<\/body>/is",$info,$str);
       $str=$str[0];
       $pm_info=[];
       $projectWeek_info=[];
       $project_info=   $project->where(["id"=>intval($project_id)])->find();
       if (!empty($project_info)) {
           $pm_info=   $member->where(["id"=>intval($project_info["pm_uid"])])->find();
           $projectWeek_info=$projectWeek->where(["id"=>$week_id,"project_id"=>$project_id,"uid"=>$this->member_id])->find();
       }else{
           $this->error("项目不存在");
       }
       $word->export($str, $pm_info["nickname"]."周报");
    }

    /**
     *  导出所有项目经理周报
     */
     public  function export_pm(){
        $week=intval(input("week"));
        if(empty($week)){
          $week=date('W',time());
        }
        $word= new WordExport();
        $request =request();
        $domain= $request->domain();
        $url= $domain."/index/common/weekpm?week=". $week;
        $info=file_get_contents($url);
        $str="";
        preg_match("/<body[^>]*?>(.*\s*?)<\/body>/is",$info,$str);
        $str=$str[0];
        $word->export($str,"第".$week."周报");
     }
}
